'use strict';

const AppPages = [
    {
        id: 0,
        link: '#!/app?url=/app/static/apps/sound_meter/',
        src: `assets/themes/${iconTheme}/icon-soundmeter.png`,
        name: '噪音计'
    },
    {
        id: 1,
        link: '#!/app?url=/app/static/apps/tuner/',
        src: `assets/themes/${iconTheme}/icon-tuner.webp`,
        name: '调音器'
    },
    // url = encodeURI(`#!/scratchRunner?src=/app/static/apps/recorder/recorder.sb3`)
    // api = encodeURI(`/system/execFile?path=/home/pi/workspace/lepi-gui/app/static/apps/camera/camera.py`)
    {
        id: 2,
        link: null,
        api: encodeURI(`/system/execFile?path=/home/pi/workspace/lepi-gui/app/static/apps/camera/camera.py`),
        src: `assets/themes/${iconTheme}/app-camera.png`,
        name: '照相机'
    },
    {
        id: 3,
        // link: '#!/app?url=' + encodeURI(`http://192.168.50.173/build/embed.html?project_url=http://192.168.50.173:8000/app/static/apps/recorder/recorder.sb3&autoplay`),
        link: encodeURI(`#!/scratchRunnerNormal?src=/app/static/apps/recorder/recorder.sb3`),
        src: `assets/themes/${iconTheme}/app-recorder.png`,
        name: '录音机'
    },
    // {
    //     id: 4,
    //     link: encodeURI(`#!/scratchRunnerNormal?src=/explore/Scratch/debug.sb3`),
    //     src: `assets/themes/${iconTheme}/icon-scratch.png`,
    //     name: '竖屏调试'
    // },
    // {
    //     id: 2,
    //     link: '#!/app?url=/app/static/apps/led_simulator/screen.html',
    //     src: `assets/themes/${iconTheme}/icon-led-simulator.png`,
    //     name: '点阵模拟器'
    // },
    {
        id: 5,
        link: null,
        api: encodeURI(`/system/execFile?path=/home/pi/workspace/lepi-gui/app/static/apps/sd_card_copier/系统克隆.sh`),
        src: `assets/themes/${iconTheme}/app-sd-card-copier.png`,
        name: '系统克隆'
    },
]

angular.module('myApp.app', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/app', {
            templateUrl: 'pages/app/app.html',
            controller: 'AppCtrl'
        })
    }])

    .controller('AppCtrl', function ($rootScope, $location) {

        let showAppList = () => {
            $rootScope.show_header = true
            $rootScope.title = '应用程序'
            $rootScope.url = ''
            $rootScope.items = AppPages
            $rootScope.rowNum = 3
            $rootScope.colNum = 2
            $rootScope.updatePageInfo()
        }

        let url = $location.search().url
        let url_path = $location.search().url_path
        console.log(url)
        if (url) {
            $rootScope.show_header = false
            $rootScope.show_footer = false
            $rootScope.items = []
            $rootScope.url = url
            if (url_path) {
                $rootScope.url = url + '#url=' + url_path
            }
            $rootScope.updatePageInfo()
            let iframe = document.getElementById('app-container').contentWindow
            iframe.onkeyup = (e) => {
                console.log(e)
                if (e.code == 'F2' || e.code == 'Esc') {
                    // showAppList()
                    // e.stopPropagation();
                    // e.preventDefault();
                    // dispatchKeyEvent('B', 'keydown')
                    // history.back();
                    iframe.parent
                    history.go(-1);
                }
            }

        } else {
            showAppList()

        }

    });